<?php
namespace Admin\Controller;
use Common\Model\CityModel;
use Common\Model\InfoContentModel;
use Think\Controller;

class CrontabController extends  Controller{

	public function crontabList(){
		$this->display();
	}


	/**
	 * 定时更新 分享置顶信息下架
	 */
	public function updateInfoType(){
		$date = date("Y-m-d H:i:s");
		$sql = "update __PREFIX__info_content set type = 0 where type = 1 and status_time < '{$date}';";
		$sql = str_replace('__PREFIX__',C('DB_PREFIX'),$sql);

		$model = M();
		$res = $model->execute($sql);
		if($res){
			$this->success("更新成功");
		}else{
			$this->error('更新失败');
		}
	}


	public function updateWeather(){
		set_time_limit(0);
		$key = C('weather_api_key');

		$cityModel = new CityModel();
		$city = $cityModel->getCityList();

		foreach($city as $k => $v){
			$url = sprintf('https://api.heweather.com/x3/weather?cityid=%s&key=%s',$v['id'],$key);
			$data = curl($url);
			//错误判断
			$weather = json_decode($data,true);
			if($weather['HeWeather data service 3.0'][0]['status'] == 'invalid key'){
				exit('错误的用户 key');
			}else if($weather['HeWeather data service 3.0'][0]['status'] == 'unknown city'){
				exit('未知城市');
			}else if($weather['HeWeather data service 3.0'][0]['status'] == 'no more requests'){
				exit('超过访问次数');
			}else if($weather['HeWeather data service 3.0'][0]['status'] == 'anr'){
				exit('服务无响应或超时');
			}else if($weather['HeWeather data service 3.0'][0]['status'] == 'permission denied'){
				exit('没有访问权限');
			}
			if($data){
				$caheKey = sprintf('weather_city_%s',$v['cid']);
				F($caheKey,$data);
				echo sprintf('国家 => 【%s】 天气请求缓存完毕%s',$v['city'],"\n<br/>");
			}
		}
	}


}
